package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportDrawingCategoryAuditDTO;
import com.eastfair.home.dto.ReportDrawingCategoryAuditPageQuery;
import com.eastfair.home.dto.ReportDrawingCategoryAuditSaveDTO;
import com.eastfair.home.dto.ReportDrawingCategoryAuditUpdateDTO;
import com.eastfair.home.entity.ReportDrawingCategoryAudit;
import com.eastfair.home.service.ReportDrawingCategoryAuditService;
import com.eastfair.home.vo.ReportDrawingCategoryAuditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆图纸分类审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportDrawingCategoryAudit")
@Api(value = "ReportDrawingCategoryAudit", tags = "报馆图纸分类审核记录")
@PreAuth(replace = "home:reportDrawingCategoryAudit:")
public class ReportDrawingCategoryAuditController extends SuperController<ReportDrawingCategoryAuditService, Long, ReportDrawingCategoryAudit, ReportDrawingCategoryAuditPageQuery, ReportDrawingCategoryAuditSaveDTO, ReportDrawingCategoryAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportDrawingCategoryAuditService reportDrawingCategoryAuditService;

    @Override
    public void handlerResult(IPage<ReportDrawingCategoryAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportDrawingCategoryAudit> reportDrawingCategoryAuditList = list
                .stream()
                .map((map) -> ReportDrawingCategoryAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportDrawingCategoryAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportDrawingCategoryAuditPageQuery> params) {
        ReportDrawingCategoryAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆审核ID查询图纸分类审核VO列表.
     *
     * @param reportSpaceAuditId the report space audit id
     * @return the r
     */
    @GetMapping("/reportSpaceAudit/{reportSpaceAuditId}")
    public R<List<ReportDrawingCategoryAuditVO>> queryByReportSpaceAuditId(@PathVariable("reportSpaceAuditId") Long reportSpaceAuditId) {
        log.info("queryByReportSpaceAuditId - 报馆审核ID查询图纸分类审核VO列表, reportSpaceAuditId={}", reportSpaceAuditId);
        List<ReportDrawingCategoryAuditVO> list
                = reportDrawingCategoryAuditService.listVOByReportSpaceAuditId(reportSpaceAuditId);
        return R.successDef(list);
    }

    /**
     * 报馆ID查询图纸分类审核VO列表.
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @ApiOperation("报馆ID查询图纸分类审核VO列表")
    @GetMapping("/reportSpace/{reportSpaceId}")
    public R<List<ReportDrawingCategoryAuditVO>> queryByReportSpaceId(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("queryByReportSpaceId - 报馆ID查询图纸分类审核VO列表, reportSpaceId={}", reportSpaceId);
        try {
            List<ReportDrawingCategoryAuditVO> list
                    = reportDrawingCategoryAuditService.listVOByReportSpaceId(reportSpaceId);
            return R.successDef(list);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新图纸分类报馆审核状态.
     *
     * @param reportDrawingCategoryAuditDTO the report drawing category audit dto
     * @return the r
     */
    @PostMapping("/update/venueAuditStatus")
    public R<Boolean> updateVenueAuditStatus(@RequestBody ReportDrawingCategoryAuditDTO reportDrawingCategoryAuditDTO) {
        log.info("updateVenueAuditStatus - 批量更新图纸分类场馆审核状态, reportDrawingCategoryAuditDTO={}", reportDrawingCategoryAuditDTO);
        try {
            return R.successDef(reportDrawingCategoryAuditService.updateVenueAuditStatus(reportDrawingCategoryAuditDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
